package com.ita.acmsystem.merchant.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.json.JSONException;
import org.json.JSONObject;

public class UrlConnUtil {

	private static String ADMIN_URL = "http://10.222.29.195:8080/amc-a/GetStatusServlet?merchantId=";

	public static Map<String, String> getLoginStatusFromAdmin(String merchantId)
			throws IOException, JSONException {
		Properties properties = new Properties();
		// properties.load(new FileInputStream("UrlConn.properties"));
		properties.load(UrlConnUtil.class
				.getResourceAsStream("/UrlConn.properties"));

		ADMIN_URL = properties.getProperty("ADMIN_URL",
				"http://10.222.29.195:8080/amc-a/GetStatusServlet?merchantId=");

		Map<String, String> map = new HashMap<String, String>();
		URL url = new URL(ADMIN_URL + merchantId);
		URLConnection conn = url.openConnection();
		conn.connect();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
		String result = "";
		String line = null;
		while ((line = br.readLine()) != null) {
			result += "\n" + line;
		}
		JSONObject jsonObject = new JSONObject(result);
		map.put("auditStatus", jsonObject.getString("auditStatus"));
		map.put("remarks", jsonObject.getString("remarks"));
		map.put("isBlack", jsonObject.getString("isBlack"));
		return map;
	}
}
